import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
/* Layout */
import Layout from '@/layout'
import employees from './modules/employees.js'
import departments from './modules/departments.js'
import settings from './modules/settings.js'
import salarys from './modules/salarys.js'
import social_securitys from './modules/social_securitys.js'
import attendances from './modules/attendances.js'
import approvals from './modules/approvals.js'
import permissions from './modules/permissions.js'

// 动态路由表，项目中不同的用户可以访问不同的功能
export const asyncRoutes = [
  departments,
  employees,
  settings,
  salarys,
  social_securitys,
  attendances,
  approvals,
  permissions
]
// 静态路由表，项目中每个用户都可以访问的功能
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: 'Dashboard', icon: 'dashboard' }
      }
    ]
  }
  // 404 page must be placed at the end !!!
  // { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () =>
  new Router({
    // 控制路由滚动行为  滚动到顶部
    scrollBehavior: () => ({ y: 0 }),
    // 组合到一起组成路由表
    // routes: [...constantRoutes, ...asyncRoutes]
    routes: [...constantRoutes]
  })

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
// 重置路由
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // 重新设置路由的可匹配路径
}

export default router
